package com.cn.hotel.dao.Impl;

import com.cn.hotel.dao.INavDao;
import com.cn.hotel.model.NavModel;
import com.cn.hotel.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NavImpl implements INavDao {
    /**
     * 查询所有
     *
     * @param navModel
     * @return
     */
    @Override
    public List<NavModel> findAll(NavModel navModel) {
        StringBuilder sql = new StringBuilder("select * from nav where 1=1");
        if (navModel.getId() != 0) {
            sql.append(" and id ='" + navModel.getId() + "'");
        }
        if (navModel.getName() != null && !"".equals(navModel.getName())) {
            sql.append(" and name ='" + navModel.getName() + "'");
        }
        sql.append(" order by id asc limit " + (navModel.getPageModel().getPageNum() - 1) * navModel.getPageModel().getPageSize() + "" +
                "," + navModel.getPageModel().getPageSize() + "");
        List<NavModel> list = new ArrayList<>();
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        try {
            while (resultSet.next()) {
                NavModel model = new NavModel();
                model.setId(resultSet.getInt("id"));
                model.setName(resultSet.getString("name"));
                model.setHref(resultSet.getString("href"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 获取数据库总条数  用来分页
     *
     * @param navModel
     * @return
     */
    @Override
    public int getCount(NavModel navModel) {
        StringBuilder sql = new StringBuilder("select count(*) count from nav where 1=1");
        if (navModel.getId() != 0) {
            sql.append(" and id ='" + navModel.getId() + "'");
        }
        if (navModel.getName() != null && !"".equals(navModel.getName())) {
            sql.append(" and name ='" + navModel.getName() + "'");
        }
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        int count = 0;
        try {
            resultSet.next();
            count = resultSet.getInt(1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 添加
     *
     * @param navModel
     * @return
     */
    @Override
    public int add(NavModel navModel) {
        StringBuilder sql = new StringBuilder();
        if (!"".equals(navModel.getName()) && navModel.getName() != null && !"".equals(navModel.getHref()) && navModel.getHref() != null) {
            sql.append("insert into nav(name,href) values('" + navModel.getName() + "','" + navModel.getHref() + "')");
        }
        return JDBC.executeUpdate(sql.toString());
    }

    //用户编辑  通过id查询 赋值到页面
    @Override
    public NavModel findById(Integer aa) {
        StringBuilder sql = new StringBuilder();
        if (aa != 0) {
            sql.append("select * from nav where id=" + aa);
        }
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        NavModel model = new NavModel();
        try {
            while (resultSet.next()) {
                model.setId(resultSet.getInt("id"));
                model.setName(resultSet.getString("name"));
                model.setHref(resultSet.getString("href"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    /**
     * 编辑 修改
     * @param navModel
     * @return
     */
    @Override
    public int editNav(NavModel navModel) {
        StringBuilder sql = new StringBuilder();
        sql.append("update  nav set");
        if (!"".equals(navModel.getName()) && navModel.getName() != null && !"".equals(navModel.getHref()) && navModel.getHref() != null) {
           sql.append(" name='"+navModel.getName()+"', href='"+navModel.getHref()+"' where id ="+navModel.getId()+"");
        }
        return JDBC.executeUpdate(sql.toString());
    }

    /**
     * 删除
     * @param temp
     * @return
     */
    @Override
    public int delete(Integer temp) {
        StringBuilder sql = new StringBuilder();
        sql.append(" delete from nav where id="+temp);
        return JDBC.executeUpdate(sql.toString());
    }

    /**
     * 下面代码是前端页面调值得实现--------------------
     * @return
     */

    /**
     * 获取导航
     * @return
     */
    @Override
    public List<NavModel> getNavModelListFromIo() {
        StringBuilder sql = new StringBuilder("select * from nav");
        List<NavModel> list = new ArrayList<>();
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        try {
            while (resultSet.next()) {
                NavModel model = new NavModel();
                model.setName(resultSet.getString("name"));
                model.setHref(resultSet.getString("href"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
